package controllers;

import helpers.Paginator;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.LinkModel;
import models.UserModel;

import data.LinkData;

public class ListLinkServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		UserModel user = (UserModel) request.getAttribute("user");
		int page = 1;
		try {
			page = Integer.parseInt((String) request.getParameter("page"));
		} catch (Exception e) {}
		
		LinkData linkData = new LinkData();
		linkData.setPage(page);
		
		ArrayList<LinkModel> links = linkData.list(user.getUsername());
		boolean nextPage = false;
		boolean prevPage = false;
		
		if (links.size() == linkData.getLimit()) {
			nextPage = true;
		}
		
		if (page > 1) {
			prevPage = true;
		}
		
		request.setAttribute("prevPage", prevPage);
		request.setAttribute("nextPage", nextPage);
		request.setAttribute("links", links);
		request.setAttribute("page", page);
		
		try {
			request.getRequestDispatcher("/pages/list_link.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
